<template>
    <view>
        <!-- pages/wallet/pay.wxml -->
        <header name="钱包充值" :type="1"></header>
        <text class="paywaytitle">充值方式</text>
        <picker @change="bindPickerChange" :value="index" :range="array">
            <view class="payway">
                <image src="/static/weixin.png" mode="aspectFit"></image>
                <text>{{ array[index] }}</text>
                <image src="/static/down.png" mode="aspectFit"></image>
            </view>
        </picker>
        <view class="payinput">
            <text>充值金额</text>
            <view class="tinput">
                <text>￥</text>
                <input type="number" @input="oninput" />
            </view>
        </view>

        <view class="paybtn" @tap="pay">确认充值</view>
    </view>
</template>

<script>
import header from '../../components/header/headerBar.vue';
// pages/wallet/pay.js
const app = getApp();
export default {
    components: {
        header
    },
    data() {
        return {
            array: ['充值金额转余额', '在线充值'],
            index: 0,
            type: 1,
            flag: true,
            money: ''
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        bindPickerChange(e) {
            this.setData({
                type: e.detail.value + 1,
                index: e.detail.value
            });
        },

        oninput(e) {
            this.setData({
                money: e.detail.value
            });
        },

        pay() {
            var _this = this;

            if (!this.money) {
                app.globalData.showtoast('请输入充值金额');
                return false;
            }

            if (this.flag) {
                _this.setData({
                    flag: false
                });

                app.globalData
                    .http()
                    .acc_chongzhi({
                        type: this.type,
                        money: this.money
                    })
                    .then((res) => {
                        _this.setData({
                            flag: true
                        });

                        if (res.status) {
                            uni.requestPayment({
                                timeStamp: res.pay.timestamp,
                                nonceStr: res.pay.nonceStr,
                                package: res.pay.package,
                                signType: 'MD5',
                                paySign: res.pay.sign,

                                success(res1) {
                                    uni.showToast({
                                        title: res1.info,
                                        icon: 'none'
                                    });
                                    uni.navigateBack({
                                        delta: 1
                                    });
                                },

                                fail(res) {
                                    uni.showToast({
                                        title: '支付取消',
                                        icon: 'none'
                                    });
                                }
                            });
                        } else {
                            app.globalData.showtoast(res.msg);
                        }
                    })
                    .catch((res) => {});
            }
        }
    }
};
</script>
<style lang="scss"> @import 'main.scss';
@import './pay.scss';
</style>
